package com.wushi.lenovo.asia5b.app_di.home.presenter;

import android.text.TextUtils;

import com.mvp.base.BasePresenter;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.app_di.home.mvpview.HomeFragmentView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.h5.H5WishBean;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import javax.inject.Inject;

import io.reactivex.Observable;


/**
 * Created by zhangyuncai on 2019/4/10.
 */
public class HomeFragmentPresenter extends BasePresenter<HomeFragmentView> {

    ApiService apiService;

    @Inject
    public HomeFragmentPresenter(ApiService apiService) {
        this.apiService = apiService;
    }

    //改变收藏状态
    public void changeWishType(H5WishBean h5WishBean) {
        boolean isWish = true;//收藏成功后的状态, true/false  收藏/非收藏
        Observable<ResultModel> observable = apiService.addWish(UserInfo.getMemberId(), h5WishBean.getGoods_id());
        if (TextUtils.equals(h5WishBean.getProduct_wishlist(), "1")) {//当前状态已收藏,那么就删除收藏
            observable = apiService.deleteWish(UserInfo.getMemberId(), h5WishBean.getGoods_id());
            isWish = false;
        }
        boolean finalIsWish = isWish;
        baseSubscribe(observable,
                resultModel -> {
                    getView().changeWishTypeResult(resultModel, h5WishBean, finalIsWish);
                });
    }

    public void getH5Version() {
        baseSubscribe(apiService.getH5Version("android"),
                resultModel -> {
                    getView().getH5VersionResult(resultModel);
                });
    }

}